import * as types from './fundPortfolioMutationTypes';
import arrayUtil from '../../../common/arrayUtil';

const mutations = {
  [types.PORTFOLIO_QUERY_PORTFOLIOS_BEGIN](state) {
    state.Portfolios = [];
  },
  [types.PORTFOLIO_QUERY_PORTFOLIOS_SUC](state, {Portfolios}) {
    state.Portfolios = Portfolios;
  },

  [types.COMBINATION_QUERY_BEGIN](state) {
    state.Combinations = [];
  },
  [types.COMBINATION_QUERY_SUC](state, {Combination}) {
    state.Combinations = Combination;
  },
  [types.PORTFOLIO_ADD_SUC](state, {Portfolio}) {
    let Portfolios = state.Portfolios || [];
    Portfolios.push(Portfolio);
    state.Portfolios = Portfolios;
  },
  [types.COMBINATION_ADD_SUC](state, {Combination}) {
    let Combinations = state.Combinations || [];
    Combinations.push(Combination);
    state.Combinations = Combinations;
  },
  [types.PORTFOLIOS_QUERY_COUNT_BEGIN](state) {

  },
  [types.PORTFOLIOS_QUERY_COUNT_SUC](state, {totalCount}) {
    state.totalCount = totalCount
  },
  [types.PORTFOLIO_UPDATE_SUC](state, {Portfolio}) {
    for (let i = 0; i < state.Portfolios.length; i++) {
      let item = state.Portfolios[i];
      if (item.id === Portfolio.id) {
        item.title = Portfolio.title;
        item.intro = Portfolio.intro;
        item.homeImgUrl = Portfolio.homeImgUrl;
        item.content = Portfolio.content;
        item.groupId = Portfolio.groupId;
        item.type = Portfolio.type;
        item.state = Portfolio.state;
        item.description = Portfolio.description;
        break;
      }
    }
  },
  [types.PORTFOLIO_DEL_SUC](state, {combinationCode}) {
    console.log(state.Portfolios)
    console.log(combinationCode)
    arrayUtil.removeItem(state.Portfolios, 'combinationCode', combinationCode);
  }
}

export default mutations;
